package com.yenlien.traceyun.breed.service;

import com.yenlien.traceyun.breed.dao.BreedDao;
import com.yenlien.traceyun.breed.dao.BreedDetailDao;
import com.yenlien.traceyun.breed.dao.FeedDao;
import com.yenlien.traceyun.breed.entity.Breed;
import com.yenlien.traceyun.breed.entity.BreedDetail;
import com.yenlien.traceyun.breed.entity.Feed;
import com.yenlien.traceyun.business.dao.BoxDao;
import com.yenlien.traceyun.business.dao.SubpackageDao;
import com.yenlien.traceyun.business.entity.Box;
import com.yenlien.traceyun.business.entity.CodeInfo;
import com.yenlien.traceyun.business.entity.Subpackage;
import com.yenlien.traceyun.business.interceptor.UserAuthUtil;
import com.yenlien.traceyun.business.service.SubpackageService;
import com.yenlien.traceyun.business.trace.TraceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: bigwangy@sina.com
 * @create: 2021-01-08
 * @description:生成追溯信息
 **/

@Service
public class BreedTraceInfoService {

    @Autowired
    private SubpackageDao subpackageDao;
    @Autowired
    private BoxDao boxDao;

    @Autowired
    private BreedDao breedDao;

    @Value("${traceyun.app.id}")
    private String appId;

    @Autowired
    private TraceService traceService;

    @Autowired
    private BreedDetailDao breedDetailDao;

    @Autowired
    private FeedDao feedDao;


    @Transactional(rollbackFor = RuntimeException.class)
    public void subGenerateCodeInfo(String breedId) {
        Breed breed = breedDao.findById(breedId);
        List<Subpackage> subs = subpackageDao.findByRelevanceId(breedId);
        if (subs.size() == 0) {
            Subpackage subpackage = new Subpackage();
            subpackage.setBatch(breed.getBatch());
            subpackage.setAppId(appId);
            subpackage.setCompanyId(breed.getCompanyId());
            subpackage.setLastStep(breedId);
            subpackage.setRelevanceId(breedId);
            subpackage.setProductName(breed.getBreedName());
            Subpackage p = subpackageDao.save(subpackage);
            List<BreedDetail> details = breedDetailDao.findByRelevanceId(breedId);
            if (details != null && details.size() > 0) {
                Date date = new Date();
                List<Box> boxs = new ArrayList<>();
                for (BreedDetail detail : details) {
                    Box box = new Box();
                    box.setAppId(appId);
                    box.setCompanyId(breed.getCompanyId());
                    box.setCreatetime(date);
                    box.setSourceCode(detail.getCode());
                    box.setSubpackageBatch(p.getBatch());
                    box.setSubpackageId(p.getId());
                    box.setCreatetime(new Date());
                    boxs.add(box);
                }
                boxDao.saveAll(boxs);
            }
            traceService.addInfo(p.getBatch(), p.getBatch(), subpackage, appId, p.getCompanyId(), "");
        }
    }


}
